<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="举报类型" prop="jblxId">
        <el-select v-model="queryParams.jblxId" placeholder="请选择举报类型id" clearable>
          <el-option
            v-for="dict in dict.type.jblx"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="相关店铺" prop="dpName">
        <el-input
          v-model="queryParams.dpName"
          placeholder="请输入相关店铺"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="举报内容" prop="jbglJbnr">
        <el-input
          v-model="queryParams.jbglJbnr"
          placeholder="请输入举报内容"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系方式" prop="yhPhone">
        <el-input
          v-model="queryParams.yhPhone"
          placeholder="请输入举报人id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="举报时间">-->
<!--        <el-date-picker-->
<!--          v-model="daterangeJbglJbsj"-->
<!--          style="width: 240px"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          type="daterange"-->
<!--          range-separator="-"-->
<!--          start-placeholder="开始日期"-->
<!--          end-placeholder="结束日期"-->
<!--        ></el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="处理时间">-->
<!--        <el-date-picker-->
<!--          v-model="daterangeJbglClsj"-->
<!--          style="width: 240px"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          type="daterange"-->
<!--          range-separator="-"-->
<!--          start-placeholder="开始日期"-->
<!--          end-placeholder="结束日期"-->
<!--        ></el-date-picker>-->
<!--      </el-form-item>-->
      <div class="block" style="float: left">
        <span class="demonstration" style="margin-right: 10px">举报时间</span>
        <el-date-picker
          v-model="value1"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          align="right">
        </el-date-picker>
      </div>

      <div class="block" style="float: left;margin-left: 125px;margin-bottom: 20px">
        <span class="demonstration" style="margin-right: 10px">处理时间</span>
        <el-date-picker
          v-model="value2"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          align="right">
        </el-date-picker>
      </div>
<!--      <el-form-item label="处理意见id" prop="jbglClyj">-->
<!--        <el-select v-model="queryParams.jbglClyj" placeholder="请选择处理意见id" clearable>-->
<!--          <el-option-->
<!--            v-for="dict in dict.type.clyj"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="处理状态" prop="ztId">
        <el-select v-model="queryParams.ztId" placeholder="请选择状态id" clearable>
          <el-option
            v-for="dict in dict.type.zt"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <br/>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

<!--    <el-row :gutter="10" class="mb8">-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['system:jubaoguanli:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['system:jubaoguanli:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['system:jubaoguanli:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['system:jubaoguanli:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->

    <el-table v-loading="loading" :data="jubaoguanliList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="流水号" align="center" prop="jbglId" />
      <el-table-column label="举报类型" align="center" prop="jblxId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.jblx" :value="scope.row.jblxId"/>
        </template>
      </el-table-column>
      <el-table-column label="相关店铺" align="center" prop="dianpu.dpName" />
      <el-table-column label="举报内容" align="center" prop="jbglJbnr" />
      <el-table-column label="联系方式" align="center" prop="yonghu.yhPhone" />
      <el-table-column label="举报时间" align="center" prop="jbglJbsj" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.jbglJbsj, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="处理时间" align="center" prop="jbglClsj" width="180">
      </el-table-column>
      <el-table-column label="处理意见id" align="center" prop="jbglClyj">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.clyj" :value="scope.row.jbglClyj"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="ztId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.zt" :value="scope.row.ztId"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope" v-if="scope.row.ztId == 1">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="chuli(scope.row)"
            v-hasPermi="['system:jubaoguanli:edit']"
          >处理</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改举报管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="举报类型id" prop="jblxId">
          <el-select v-model="form.jblxId" placeholder="请选择举报类型id">
            <el-option
              v-for="dict in dict.type.jblx"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="相关店铺id" prop="dpId">
          <el-input v-model="form.dpId" placeholder="请输入相关店铺id" />
        </el-form-item>
        <el-form-item label="举报内容" prop="jbglJbnr">
          <el-input v-model="form.jbglJbnr" placeholder="请输入举报内容" />
        </el-form-item>
        <el-form-item label="举报人id" prop="yhId">
          <el-input v-model="form.yhId" placeholder="请输入举报人id" />
        </el-form-item>
        <el-form-item label="举报时间" prop="jbglJbsj">
          <el-date-picker clearable
            v-model="form.jbglJbsj"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择举报时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="处理时间" prop="jbglClsj">
          <el-date-picker clearable
            v-model="form.jbglClsj"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择处理时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="处理意见id" prop="jbglClyj">
          <el-select v-model="form.jbglClyj" placeholder="请选择处理意见id">
            <el-option
              v-for="dict in dict.type.clyj"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态id" prop="ztId">
          <el-select v-model="form.ztId" placeholder="请选择状态id">
            <el-option
              v-for="dict in dict.type.zt"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listJubaoguanli, getJubaoguanli, delJubaoguanli, addJubaoguanli, updateJubaoguanli } from "@/api/system/jubaoguanli";

export default {
  name: "Jubaoguanli",
  dicts: ['clyj', 'zt', 'jblx'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 举报管理表格数据
      jubaoguanliList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态id时间范围
      daterangeJbglJbsj: [],
      // 状态id时间范围
      daterangeJbglClsj: [],
      //查询参数
      // queryParams: {
      //   pageNum: 1,
      //   pageSize: 10,
      //   jblxId: null,
      //   dpId: null,
      //   jbglJbnr: null,
      //   yhId: null,
      //   jbglJbsj: null,
      //   jbglClsj: null,
      //   jbglClyj: null,
      //   ztId: null,
      // },

      queryParams: {
        pageNum: 1,
        pageSize: 10,
        jblxId: null,
        dpName: null,
        jbglJbnr: null,
        yhPhone: null,
        ztId: null,
      },
      value1:'',
      value2:'',

      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  watch: {
    '$route'(to, from) {
      // 检查是否从某个特定页面返回
      if (from.path === '/system/jubaochuli') {
        this.getList();
      }
    }
  },
  methods: {
    /** 查询举报管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.value1 && '' != this.value1) {
        this.queryParams.params["beginJbglJbsj"] = this.getGoodTime(this.value1[0]);
        this.queryParams.params["endJbglJbsj"] = this.getGoodTime(this.value1[1]);
      }
      if (null != this.value2 && '' != this.value2) {
        this.queryParams.params["beginJbglClsj"] = this.getGoodTime(this.value2[0]);
        this.queryParams.params["endJbglClsj"] = this.getGoodTime(this.value2[1]);
      }
      listJubaoguanli(this.queryParams).then(response => {
        this.jubaoguanliList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        jbglId: null,
        jblxId: null,
        dpId: null,
        jbglJbnr: null,
        yhId: null,
        jbglJbsj: null,
        jbglClsj: null,
        jbglClyj: null,
        ztId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.value1 = '';
      this.value2 = '';
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.jbglId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加举报管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const jbglId = row.jbglId || this.ids
      getJubaoguanli(jbglId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改举报管理";
      });
    },
    /** 处理按钮操作 */
    chuli(row){
      localStorage.setItem('jubaochuli', JSON.stringify(row));
      this.$router.push("/system/jubaochuli");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.jbglId != null) {
            updateJubaoguanli(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addJubaoguanli(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const jbglIds = row.jbglId || this.ids;
      this.$modal.confirm('是否确认删除举报管理编号为"' + jbglIds + '"的数据项？').then(function() {
        return delJubaoguanli(jbglIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/jubaoguanli/export', {
        ...this.queryParams
      }, `jubaoguanli_${new Date().getTime()}.xlsx`)
    },
    //时间格式转化
    getGoodTime(time) {
      // 尝试将输入字符串解析为Date对象
      let date = new Date(time);
      // 检查Date对象是否有效（即不是Invalid Date）
      if (isNaN(date.getTime())) {
        throw new Error('Invalid date string');
      }
      // 提取年、月、日、时、分、秒
      // 月份从0开始，所以要加1；日期、小时、分钟、秒直接使用
      // 使用padStart来确保月份、日期、小时、分钟、秒始终是两位数
      const year = time.getFullYear();
      const month = String(time.getMonth() + 1).padStart(2, '0');
      const day = String(time.getDate()).padStart(2, '0');
      const hours = String(time.getHours()).padStart(2, '0');
      const minutes = String(time.getMinutes()).padStart(2, '0');
      const seconds = String(time.getSeconds()).padStart(2, '0');

      // 拼接成所需的格式
      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    }
  }
};
</script>
